home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / book / project / animatio / graphics.jav < prev    next >
Encoding:
Text File  |  1995-10-08  |  2.7 KB  |  149 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class GraphicsApplet extends Applet implements Runnable {
  5.     boolean    stopped = true;
  6.     Thread    animator;
  7.     int        cx = 0;
  8.     int        cy = 0;
  9.     Formula    formula;
  10.  
  11.     public void init() {
  12.     Rectangle r = bounds();
  13.     //formula = new Line(r, r.width - 50, 0, 1, 2);
  14.     //formula = new Parabola(r, r.width - 50, 0);
  15.     int xvalues[] = new int[4];
  16.     int yvalues[] = new int[4];
  17.  
  18.     xvalues[0] = 0;
  19.     yvalues[0] = 0;
  20.  
  21.     xvalues[1] = 50;
  22.     yvalues[1] = 20;
  23.  
  24.     xvalues[2] = 100;
  25.     yvalues[2] = 100;
  26.  
  27.     xvalues[3] = 50;
  28.     yvalues[3] = 20;
  29.  
  30.     formula = new Path(r, xvalues, yvalues);
  31.     animator = new Thread(this);
  32.     }
  33.     public void start() {
  34.     if (! animator.isAlive()) {
  35.         animator.start();
  36.     } else {
  37.         animator.resume();
  38.     }
  39.     }
  40.     public void stop() {
  41.     animator.suspend();
  42.     }
  43.     public void destroy() {
  44.     animator.stop();
  45.     }
  46.     public void paint(Graphics g) {
  47.     if (animator.isAlive()) {
  48.         g.setColor(Color.red);
  49.         g.drawRect(cx, cy, 50, 50);
  50.     }
  51.     }
  52.  
  53.     public void run() {
  54.     try {
  55.         while (true) {
  56.         Dimension d = formula.step();
  57.         cx = d.width;
  58.         cy = d.height;
  59.         repaint();
  60.         Thread.sleep(200);
  61.         }
  62.     } catch (InterruptedException e) {
  63.         return;
  64.     }
  65.     }
  66. }
  67.  
  68. abstract class Formula {
  69.     Rectangle    bounds;
  70.     int x = 0;
  71.     int y = 0;
  72.  
  73.     protected Formula(Rectangle bounds) {
  74.     this.bounds = bounds;
  75.     }
  76.     public abstract Dimension step();
  77. }
  78.  
  79. class Line extends Formula {
  80.     int m;
  81.     int b;
  82.     int maxStep;
  83.     int minStep;
  84.  
  85.     public Line(Rectangle bounds, int maxStep, int minStep, int m, int b) {
  86.     super(bounds);
  87.     this.m = m;
  88.     this.b = b;
  89.     this.maxStep = maxStep;
  90.     this.minStep = minStep;
  91.     }
  92.  
  93.     public Dimension step() {
  94.     x += b;
  95.     y = (m * x) + b;
  96.     if (x > maxStep || x < minStep) {
  97.         b *= -1;
  98.     }
  99.     return new Dimension(x, y);
  100.     }
  101. }
  102.  
  103. class Path extends Formula {
  104.     int i = 0;
  105.     int c = 1;
  106.     int xvalues[];
  107.     int yvalues[];
  108.     public Path(Rectangle bounds, int xvalues[], int yvalues[]) {
  109.     super(bounds);
  110.     this.xvalues = xvalues;
  111.     this.yvalues = yvalues;
  112.     }
  113.     public Dimension step() {
  114.     Dimension d = new Dimension(xvalues[i], yvalues[i]);
  115.  
  116.     i += c;
  117.     if (i == xvalues.length || i < 0) {
  118.         c *= -1;
  119.         i += c;
  120.     }
  121.  
  122.     return d;
  123.     }
  124. }
  125.  
  126. class Parabola extends Formula {
  127.     int c = 4;
  128.     int mid;
  129.     int maxStep;
  130.     int minStep;
  131.  
  132.     public Parabola(Rectangle bounds, int maxStep, int minStep) {
  133.     super(bounds);
  134.     this.maxStep = maxStep;
  135.     this.minStep = minStep;
  136.     mid = bounds.width / 4;
  137.     x = -mid;
  138.     }
  139.     public Dimension step() {
  140.     x += c;
  141.     y = (x*x) / 10;
  142.     if ((x+(2*mid)) > maxStep || (x+(2*mid)) < minStep) {
  143.         c *= -1;
  144.     }
  145.     System.out.println((x+mid) + " " + x + ", " + y);
  146.     return new Dimension(x + mid + mid, y);
  147.     }
  148. }
  149.